#include "selectcollectiondialog.h"
#include "tabmodel.h"

SelectCollectionDialog::SelectCollectionDialog( QStringList l, bool filter_set )
{
setupUi(this);

    if ( true == filter_set )
    {
    filterRadioButton->setChecked(true);
    }
    else
    {
    filterRadioButton->setDisabled(true);
    }

comboBox->addItems(l);

    {
    QSqlDatabase db = QSqlDatabase::database("collection", false);
        if ( !db.isOpen() )
        {
        QMessageBox::warning(this,
                             tr("Error" ),
                             tr("Database is closed.") );
        return;
        }

    TabModel * model = new TabModel(0, db);
    model->setEditStrategy(TabModel::OnFieldChange);
    model->setTable("albums_reg");
        if (model->lastError().type() != QSqlError::NoError)
        {
        QMessageBox::warning(this,
                             tr("Error"),
                             model->lastError().text() );
        return;
        }
    
    model->select();
    model->setHeaderData(2, Qt::Horizontal, tr("Collection") );
    model->setHeaderData(3, Qt::Horizontal, tr("Name") );
        if (model->lastError().type() != QSqlError::NoError)
        {
        QMessageBox::warning(this,
                             tr("Warning"),
                             model->lastError().text() );
        }
    
    tableView->setModel(model);
    tableView->setColumnWidth(3,240);
    tableView->hideColumn(0);
    tableView->hideColumn(1);
    tableView->hideColumn(4);
    }

QObject::connect( tableView,
                  SIGNAL( clicked(QModelIndex) ),
                  this,
                  SLOT( select_album(QModelIndex) ) );
}

SelectCollectionDialog::~SelectCollectionDialog()
{
}

void SelectCollectionDialog::closeEvent( QCloseEvent * e )
{
delete_model();
e->accept();
}

void SelectCollectionDialog::select_album(QModelIndex index)
{
m.clear();
TabModel * model = qobject_cast<TabModel *>(tableView->model());
QSqlRecord record = model->record( index.row() );
m["table"] = record.value("table_name").toString();
m["num"]   = record.value("album_num").toString();
m["name"]  = record.value("album_name").toString();
QString num_bold = QLatin1String("<b>");
num_bold.append( m.value("num") );
num_bold.append( QLatin1String("</b>") );
albumLabel->setText(num_bold);
return;
}

QMap<QString, QString> SelectCollectionDialog::return_selected()
{
    if ( true == allRadioButton->isChecked() )
    {
    m["items"] = QLatin1String("ALL");
    }

    if ( true == filterRadioButton->isChecked() )
    {
    m["items"] = QLatin1String("FILTER");
    }

    if ( true == statusRadioButton->isChecked() )
    {
    m["items"] = QLatin1String("STATUS");
    m["status"] = comboBox->currentText();
    }

    if ( true == currentCheckBox->isChecked() )
    {
    m["use_current"] = QLatin1String("TRUE");
    m["table"] = QLatin1String("");
    m["num"]   = QLatin1String("");
    m["name"]  = QLatin1String("");
    return m;
    }

return m;
}

void SelectCollectionDialog::delete_model()
{
TabModel * old_model = qobject_cast<TabModel *>(tableView->model());
tableView->setModel(new TabModel);    
    if (NULL != old_model)
    {
    old_model->clear();
    }
delete old_model;
old_model = NULL;
return;
}
